<?PHP 
class userlib{
    
    function login($user,$password){
        $CI =& get_instance();
        $CI->load->library('session','','session');
        
        $login=$CI->user_model->loginCheck($user,$password);
        
        if($login!=null){
            if($login[0]->current_status!="locked"){
                date_default_timezone_set('Asia/Singapore');
                $data['lastLoggedIn']=time();
                $CI->db->where("id",$login[0]->id);
                $CI->db->update("user",$data);
               //add User Id into the session
               $CI->session->set_userdata('user_id',$login[0]->id);
               //add cust_id into the session
               $CI->session->set_userdata('email',$login[0]->userName);
               $CI->session->set_userdata('login',true);
               
               $CI->session->set_userdata('type_id',$login[0]->type_id);
               
               return $login[0]->id;
            }
            else{
                return "locked";
            }
        }
        else{
            return 0;
        }
        
    }
    
    function findUser($user){
        $CI =& get_instance();
        $CI->load->model('user_model',"user",true);
        $user=$CI->user->findUser($user);
        if($user!=null){
            if($user[0]->current_status=="locked"){
                return "locked";
            }
            return $user[0]->id;
        }
    }
    
    function lockacct($user){
        $CI =& get_instance();
        
        $CI->load->library('session','','session');
        if($CI->user_model->lock($user)){
            return true;
        }
        else{
            return false;
        }
    }
    
    function setSessions($sessions){
        $CI =& get_instance();
        $CI->load->library('session','','session');
//        header('P3P: CP="CAO PSA OUR"');
        foreach($sessions as $key=>$value){
           $CI->session->set_userdata($key,$value);
        }
        
    }
    
    function checkLogin(){
        $CI =& get_instance();
        $CI->load->library('session','','session');
        $user=$CI->session->userdata('user_id');
        
        if($user!=NULL){
            $query=$CI->db->get_where("user",array('id'=>$user));
            $result=$query->result();
            if($result[0]->current_status=="locked"){
                $CI->session->sess_destroy();
                return NULL;
            }
            else{
                return $user;
            }
        }
        else{
            return NULL;
        }
    }
    
    function getSessionData(){
        $CI =& get_instance();
        $CI->load->library('session','','session');

        $data=array();
        $data['user_id']=$CI->session->userdata('user_id');
        $data['email']=$CI->session->userdata('email');
        $data['ss_id']=$CI->session->userdata('ss_id');
        $data['type_id']=$CI->session->userdata('type_id');
        return $data;
    }
    
    function checkType($targettype){
        $CI =& get_instance();
        $CI->load->library('session','','session');
        $user=$CI->session->userdata('userName');
        $type=$CI->session->userdata('userType');
        if($user!=NULL && $type=$targettype || $type="Both"){
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
    
    function returnType(){
        $CI =& get_instance();
        $CI->load->library('session','','session');
        $type=$CI->session->userdata('userType');
        
        return $type;
    }
    
    function generateUniqueKey($length){
           $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = '';
            for ($i = 0; $i < $length; $i++) {
                $randomString .= $characters[rand(0, strlen($characters) - 1)];
            }
            return $randomString;
    }
    
}
